package com.hyyt.shopjsp.basic.controller;

import com.hyyt.shopjsp.basic.pojo.BasicArea;
import com.hyyt.shopjsp.basic.service.IBasicAreaService;
import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.product.pojo.ShopProductType;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;
import com.hyyt.shopjsp.util.tree.NodeUtil;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Copyright (c) 2017 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author Wang Youchun on 2017/3/20 0024上午 10:37.
 */
@Controller
@RequestMapping("/back/area")
public class BasicAreaController extends BaseController {
	private Logger log = Logger.getLogger(BasicAreaController.class);
	private String basePath = "/back/basic/area/";

	@Resource
	private IBasicAreaService basicAreaService;

	/**
	 * 进入地区信息页面
	 *
	 * @return 进入指定页面
	 */
	@RequestMapping("gotoAreaPage")
	public String gotoAreaPage(){
		return basePath+"areaList";
	}


	/**
	 * 获得地区树节点
	 * @return
	 */
	@ResponseBody
	@RequestMapping("getNodes")
	public void getNodes(HttpServletResponse response){
		Map<String, Object> param = new HashMap<>();
		String [] selectColmunsArray={"areaId","name","parentId","isLeaf"};
		param.put("selectColmunsArray",selectColmunsArray);
		param.put("sortColumns", "sortCode asc");
		List<BasicArea> areaList=basicAreaService.queryAllBasicArea(param);
		List nodeList = new ArrayList();
		HashMap nodeMap = new HashMap();
		for(int i = 0; i < areaList.size(); i++) {
			BasicArea ba = areaList.get(i);
			nodeMap = new HashMap();
			nodeMap.put("id",ba.getAreaId());
			nodeMap.put("text", ba.getName());
			nodeMap.put("isLeaf", ba.getIsLeaf());
			String parentId = String.valueOf(ba.getParentId());
			if (Utils.objectIsEmpty(parentId)) {
				parentId = "0";
			}
			nodeMap.put("parentId",parentId);
			nodeList.add(nodeMap);
		}
		//获取分类的json字符串信息
		String jsonInfo= NodeUtil.getNodeJson(nodeList,"地区信息");
		ResultMsg.create(jsonInfo, response, false);
	}

	/**
	 * 查询指定地区下的子地区列表
	 *
	 * @param page
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "listSubAreaByParentId", method = RequestMethod.POST)
	@ResponseBody
	public void listSubAreaByParentId(Integer parentId, String name, Page page, HttpServletResponse response) {
		Map<String, Object> param = new HashMap<>();
		if (Utils.objectIsNotEmpty(parentId)) {
			param.put("parentId", String.valueOf(parentId));
		}else{//默认为所有一级节点
			param.put("parentId","0");
		}

		if (Utils.objectIsNotEmpty(name)) {
			param.put("name", name);
		}
		int totalRecordCount = basicAreaService.countBasicArea(param);
		//定义查询的字段
		String [] selectColmunsArray={"areaId","name","parentId","sortCode"};
		param.put("selectColmunsArray",selectColmunsArray);
		param.put("sortColumns", "sortCode asc");
		page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
		List<BasicArea> list = basicAreaService.queryBasicArea(param, page.getPageRecordBeginIndex(), page.getPageSize());
		param.clear();
		param.put("total", totalRecordCount);// total键 存放总记录数，必须的
		param.put("rows", list);// rows键 存放每页记录 list
		param.put("parentId", parentId);
		ResultMsg.create(param, response);  //把内容发送到页面
	}

	/**
	 * 根据分类id获得分类对象
	 * @param areaId
	 * @return
	 */
	@RequestMapping(value="getBasicAreaObject",method=RequestMethod.POST)
	@ResponseBody
	public ResultMsg getBasicAreaObject(Integer areaId){
		if(Utils.objectIsNotEmpty(areaId)){
			BasicArea basicArea = basicAreaService.loadBasicArea(areaId);
			return ResultMsg.create(true, null, FastJsonUtils.toJSONString(basicArea));
		}else{
			return ResultMsg.create(false);
		}

	}

	/**
	 * 保存或修改地区
	 * @param basicArea
	 * @return
	 */
	@RequestMapping(value="saveOrUpdateBasicArea",method=RequestMethod.POST)
	@ResponseBody
	public ResultMsg saveOrUpdateBasicArea(BasicArea basicArea){
		return basicAreaService.saveOrUpdateBasicArea(basicArea);
	}

	/**
	 * 删除地区
	 * @param areaId
	 * @return
	 */
	@RequestMapping(value="deleteBasicArea",method=RequestMethod.POST)
	@ResponseBody
	public ResultMsg deleteBasicArea(Integer areaId){
		return basicAreaService.deleteBasicAreaById(areaId);
	}

	/**
	 * 查询指定地区分类下的子分类列表-供非分页功能使用
	 *
	 * @param parentId 父分类ID
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "getSubAreaListByParentId", method = RequestMethod.POST)
	@ResponseBody
	public void getSubAreaListByParentId(Integer parentId,HttpServletResponse response) {
		Map<String, Object> param = new HashMap<>();
		if (Utils.objectIsNotEmpty(parentId)) {
			param.put("parentId", String.valueOf(parentId));
		}else{//默认为所有一级节点
			param.put("parentId","0");
		}
		//定义查询的字段
		String [] selectColmunsArray={"areaId","name","parentId","sortCode","levelCode"};
		param.put("selectColmunsArray",selectColmunsArray);
		param.put("sortColumns", "sortCode asc");
		List<BasicArea> areaList = basicAreaService.queryBasicArea(param);
		param.clear();
		param.put("areaList", areaList);
		ResultMsg.create(param, response);  //把内容发送到页面
	}


}